package com.wngbms;

import lombok.extern.slf4j.Slf4j;
import rx.Observable;
import rx.Observer;
import rx.functions.Action1;
import rx.functions.Func0;

@Slf4j
public class DeferNestDemo {

    public static void main(String[] args) {
        Observable<String> observable = Observable.defer(new Func0<Observable<String>>() {
            @Override
            public Observable<String> call() {
                log.info("1 defer");
                return Observable.defer(new Func0<Observable<String>>() {
                    @Override
                    public Observable<String> call() {
                        log.info("1.1   defer");
                        return Observable.defer(new Func0<Observable<String>>() {
                            @Override
                            public Observable<String> call() {
                                log.info("1.1.1 defer");
                                return Observable.just("windows", "linux", "macos");
                            }
                        }).doOnNext(new Action1<String>() {
                            @Override
                            public void call(String s) {
                                log.info("1.1.2 doOnNext {}", s);
                            }
                        });
                    }
                }).doOnEach(new Observer<String>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(String s) {
                        log.info("1.2  onNext {}", s);
                    }
                }).doOnNext(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        log.info("1.3  doOnNext {}", s);
                    }
                });
            }
        });

        observable.subscribe(new Observer<String>() {
            @Override
            public void onCompleted() {
                log.info("3 onCompleted");
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(String s) {
                log.info("2 onNext {}", s);
            }
        });
    }
}
/*

2023-02-16 21:57:01.096 [main] [INFO ] com.wngbms.DeferNestDemo.call[16] - 1 defer
2023-02-16 21:57:01.096 [main] [INFO ] com.wngbms.DeferNestDemo.call[20] - 1.1   defer
2023-02-16 21:57:01.096 [main] [INFO ] com.wngbms.DeferNestDemo.call[24] - 1.1.1 defer

2023-02-16 21:57:01.096 [main] [INFO ] com.wngbms.DeferNestDemo.call[30] - 1.1.2 doOnNext windows
2023-02-16 21:57:01.096 [main] [INFO ] com.wngbms.DeferNestDemo.onNext[47] - 1.2  onNext windows
2023-02-16 21:57:01.096 [main] [INFO ] com.wngbms.DeferNestDemo.call[52] - 1.3  doOnNext windows
2023-02-16 21:57:01.096 [main] [INFO ] com.wngbms.DeferNestDemo.onNext[71] - 2 onNext windows

2023-02-16 21:57:01.096 [main] [INFO ] com.wngbms.DeferNestDemo.call[30] - 1.1.2 doOnNext linux
2023-02-16 21:57:01.096 [main] [INFO ] com.wngbms.DeferNestDemo.onNext[47] - 1.2  onNext linux
2023-02-16 21:57:01.096 [main] [INFO ] com.wngbms.DeferNestDemo.call[52] - 1.3  doOnNext linux
2023-02-16 21:57:01.096 [main] [INFO ] com.wngbms.DeferNestDemo.onNext[71] - 2 onNext linux

2023-02-16 21:57:01.096 [main] [INFO ] com.wngbms.DeferNestDemo.call[30] - 1.1.2 doOnNext macos
2023-02-16 21:57:01.096 [main] [INFO ] com.wngbms.DeferNestDemo.onNext[47] - 1.2  onNext macos
2023-02-16 21:57:01.096 [main] [INFO ] com.wngbms.DeferNestDemo.call[52] - 1.3  doOnNext macos
2023-02-16 21:57:01.096 [main] [INFO ] com.wngbms.DeferNestDemo.onNext[71] - 2 onNext macos

2023-02-16 21:57:01.096 [main] [INFO ] com.wngbms.DeferNestDemo.onCompleted[61] - 3 onCompleted

 */